#!/bin/bash

exit_func() {
    if  [ -d $dir_tmp ]; then
        rm -rf $dir_tmp 
    fi
}

dir_tmp=./.perf_tmp_
if [ ! -d $dir_tmp ]; then
    mkdir $dir_tmp
fi

if [ $# -eq 0 ]; then
    echo "usage: $0 command"
    exit_func
    exit 1
fi

perf record -o $dir_tmp/perf.data -F 200 -g $@
perf script -i $dir_tmp/perf.data > $dir_tmp/out.perf
stackcollapse-perf.pl $dir_tmp/out.perf > $dir_tmp/out.folded
flamegraph.pl $dir_tmp/out.folded > perf.svg

exit_func
